/*
 * 文件名：MemcachedCacheManager.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：yangl
 * 修改时间：2016-1-10
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.prisys.memcached;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.rubyeye.xmemcached.MemcachedClient;

import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;

public class MemcachedCacheManager extends
		AbstractTransactionSupportingCacheManager {

	private ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>();   
    private Map<String, Integer> expireMap = new HashMap<String, Integer>();   
  
    private MemcachedClient memcachedClient;   
  
    public MemcachedCacheManager()   
    {   
    }   
  
    @Override  
    protected Collection<? extends Cache> loadCaches()   
    {   
        Collection<Cache> values = cacheMap.values();   
        return values;   
    }   
  
    @Override  
    public MemcachedCache getCache(String name)   
    {   
        MemcachedCache cache = (MemcachedCache)cacheMap.get(name);
        if(cache == null)
        {
            Integer expire = (Integer)expireMap.get(name);
            if(expire == null)
            {
                expire = 0;
                expireMap.put(name, expire);
            }
            cache = new MemcachedCache(name, expire.intValue(), memcachedClient);
            cacheMap.put(name, cache);
        }
        return cache; 
    }   
  
    public void setMemcachedClient(MemcachedClient memcachedClient)   
    {   
        this.memcachedClient = memcachedClient;   
    }   
  
    public void setConfigMap(Map<String, Integer> configMap)   
    {   
        this.expireMap = configMap;   
    }   

}
